home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / domacnost a kancelar / joomla / Joomla_1.5.4-Stable-Full_Package.exe / plugins / search / weblinks.php < prev    next >
PHP Script  |  2008-07-06  |  4KB  |  142 lines

  1. <?php
  2. /**
  3.  * @version        $Id: weblinks.php 10381 2008-06-01 03:35:53Z pasamio $
  4.  * @package        Joomla
  5.  * @copyright    Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  6.  * @license        GNU/GPL, see LICENSE.php
  7.  * Joomla! is free software. This version may have been modified pursuant
  8.  * to the GNU General Public License, and as distributed it includes or
  9.  * is derivative of works licensed under the GNU General Public License or
  10.  * other free or open source software licenses.
  11.  * See COPYRIGHT.php for copyright notices and details.
  12.  */
  13.  
  14. // no direct access
  15. defined( '_JEXEC' ) or die( 'Restricted access' );
  16.  
  17. $mainframe->registerEvent( 'onSearch', 'plgSearchWeblinks' );
  18. $mainframe->registerEvent( 'onSearchAreas', 'plgSearchWeblinksAreas' );
  19.  
  20. JPlugin::loadLanguage( 'plg_search_weblinks' );
  21.  
  22. /**
  23.  * @return array An array of search areas
  24.  */
  25. function &plgSearchWeblinksAreas() {
  26.     static $areas = array(
  27.         'weblinks' => 'Weblinks'
  28.     );
  29.     return $areas;
  30. }
  31.  
  32. /**
  33. * Weblink Search method
  34. *
  35. * The sql must return the following fields that are used in a common display
  36. * routine: href, title, section, created, text, browsernav
  37. * @param string Target search string
  38. * @param string mathcing option, exact|any|all
  39. * @param string ordering option, newest|oldest|popular|alpha|category
  40.  * @param mixed An array if the search it to be restricted to areas, null if search all
  41.  */
  42. function plgSearchWeblinks( $text, $phrase='', $ordering='', $areas=null )
  43. {
  44.     $db        =& JFactory::getDBO();
  45.     $user    =& JFactory::getUser();
  46.  
  47.     require_once(JPATH_SITE.DS.'components'.DS.'com_weblinks'.DS.'helpers'.DS.'route.php');
  48.  
  49.     if (is_array( $areas )) {
  50.         if (!array_intersect( $areas, array_keys( plgSearchWeblinksAreas() ) )) {
  51.             return array();
  52.         }
  53.     }
  54.  
  55.     // load plugin params info
  56.      $plugin =& JPluginHelper::getPlugin('search', 'weblinks');
  57.      $pluginParams = new JParameter( $plugin->params );
  58.  
  59.     $limit = $pluginParams->def( 'search_limit', 50 );
  60.  
  61.     $text = trim( $text );
  62.     if ($text == '') {
  63.         return array();
  64.     }
  65.     $section     = JText::_( 'Web Links' );
  66.  
  67.     $wheres     = array();
  68.     switch ($phrase)
  69.     {
  70.         case 'exact':
  71.             $text        = $db->Quote( '%'.$db->getEscaped( $text, true ).'%', false );
  72.             $wheres2     = array();
  73.             $wheres2[]     = 'LOWER(a.url) LIKE '.$text;
  74.             $wheres2[]     = 'LOWER(a.description) LIKE '.$text;
  75.             $wheres2[]     = 'LOWER(a.title) LIKE '.$text;
  76.             $where         = '(' . implode( ') OR (', $wheres2 ) . ')';
  77.             break;
  78.  
  79.         case 'all':
  80.         case 'any':
  81.         default:
  82.             $words     = explode( ' ', $text );
  83.             $wheres = array();
  84.             foreach ($words as $word)
  85.             {
  86.                 $word        = $db->Quote( '%'.$db->getEscaped( $word, true ).'%', false );
  87.                 $wheres2     = array();
  88.                 $wheres2[]     = 'LOWER(a.url) LIKE '.$word;
  89.                 $wheres2[]     = 'LOWER(a.description) LIKE '.$word;
  90.                 $wheres2[]     = 'LOWER(a.title) LIKE '.$word;
  91.                 $wheres[]     = implode( ' OR ', $wheres2 );
  92.             }
  93.             $where     = '(' . implode( ($phrase == 'all' ? ') AND (' : ') OR ('), $wheres ) . ')';
  94.             break;
  95.     }
  96.  
  97.     switch ( $ordering )
  98.     {
  99.         case 'oldest':
  100.             $order = 'a.date ASC';
  101.             break;
  102.  
  103.         case 'popular':
  104.             $order = 'a.hits DESC';
  105.             break;
  106.  
  107.         case 'alpha':
  108.             $order = 'a.title ASC';
  109.             break;
  110.  
  111.         case 'category':
  112.             $order = 'b.title ASC, a.title ASC';
  113.             break;
  114.  
  115.         case 'newest':
  116.         default:
  117.             $order = 'a.date DESC';
  118.     }
  119.  
  120.     $query = 'SELECT a.title AS title, a.description AS text, a.date AS created,'
  121.     . ' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(\':\', a.id, a.alias) ELSE a.id END as slug, '
  122.     . ' CASE WHEN CHAR_LENGTH(b.alias) THEN CONCAT_WS(\':\', b.id, b.alias) ELSE b.id END as catslug, '
  123.     . ' CONCAT_WS( " / ", '.$db->Quote($section).', b.title ) AS section,'
  124.     . ' "1" AS browsernav'
  125.     . ' FROM #__weblinks AS a'
  126.     . ' INNER JOIN #__categories AS b ON b.id = a.catid'
  127.     . ' WHERE ('. $where .')'
  128.     . ' AND a.published = 1'
  129.     . ' AND b.published = 1'
  130.     . ' AND b.access <= '.(int) $user->get( 'aid' )
  131.     . ' ORDER BY '. $order
  132.     ;
  133.     $db->setQuery( $query, 0, $limit );
  134.     $rows = $db->loadObjectList();
  135.  
  136.     foreach($rows as $key => $row) {
  137.         $rows[$key]->href = WeblinksHelperRoute::getWeblinkRoute($row->slug, $row->catslug);
  138.     }
  139.  
  140.     return $rows;
  141. }
  142.